package com.realtybaron.api;

import com.google.inject.Singleton;
import com.realtybaron.auction.PostAuction;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.LoggingFilter;
import com.sun.jersey.core.util.MultivaluedMapImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import java.io.IOException;

/**
 * User: marc Date: Feb 3, 2010 Time: 7:20:35 AM
 * <p/>
 * THIS SOFTWARE IS COPYRIGHTED.  THE SOFTWARE MAY NOT BE COPIED REPRODUCED, TRANSLATED, OR REDUCED TO ANY ELECTRONIC MEDIUM OR MACHINE READABLE FORM WITHOUT THE PRIOR WRITTEN CONSENT OF SOCO
 * TECHNOLOGIES.
 */
@Singleton
public class PostSellerController extends AbstractWebController {
  /**
   * @param req web request
   * @param res web response
   * @throws ServletException
   * @throws IOException
   */
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    this.resolver.handle(req, res, "seller");
  }
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    try {
      Client client = Client.create();
      client.addFilter(new LoggingFilter(System.out));
      WebResource resource = client.resource(this.apiBase + "/lead/listing");
      MultivaluedMap<String, String> params = new MultivaluedMapImpl();
      params.add("fname", req.getParameter("fname"));
      params.add("lname", req.getParameter("lname"));
      params.add("email", req.getParameter("email"));
      params.add("daytime", req.getParameter("daytime"));
      params.add("evening", req.getParameter("evening"));
      params.add("property_type", req.getParameter("property_type"));
      params.add("address", req.getParameter("address"));
      params.add("zip", req.getParameter("zip"));
      params.add("beds", req.getParameter("beds"));
      params.add("baths", req.getParameter("baths"));
      params.add("size", req.getParameter("size"));
      params.add("estimate", req.getParameter("estimate"));
      params.add("bid_format", req.getParameter("bid_format"));
      params.add("bid_quantity", req.getParameter("bid_quantity"));
      params.add("opening_bid", req.getParameter("opening_bid"));
      params.add("reserve_bid", req.getParameter("reserve_bid"));
      params.add("stop_bid", req.getParameter("stop_bid"));
      params.add("min_rank", req.getParameter("min_rank"));
      params.add("referral_fee", "$0");
      params.add("referral_format", "flat_fee");
      params.add("open_url", this.appBase + "/OpenAuction.htm?id={0}");
      params.add("view_url", this.appBase + "/ViewAuction.htm?id={0}");
      params.add("qna_url", this.appBase + "/ViewInquiry.htm?id={0}");
      params.add("bid_url", this.appBase + "/ViewBid.htm?id={0}");
      params.add("bids_url", this.appBase + "/ListBids.htm?id={0}");
      params.add("redo_url", this.appBase + "/PostSeller.htm");
      params.add("api_key", this.apiKey);
      params.add("app_name", this.appName);
      PostAuction result = resource.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_XML).post(PostAuction.class, params);
      if (result.getStatus().getCode() == HttpServletResponse.SC_OK) {
        this.resolver.handle(req, res, "activation");
      } else {
        req.setAttribute("messages", result.getStatus().getMessages().getMessage());
        this.resolver.handle(req, res, "seller");
      }
    } catch (Exception e) {
      throw new ServletException(e.getMessage(), e);
    }
  }
}
